class Main extends eui.Component {
    public constructor() {
        super();
        SceneManager.instance.setRoot(this);
        this.addEventListener(eui.UIEvent.ADDED_TO_STAGE, this.onAddedToStage, this);
        this.addEventListener(egret.Event.COMPLETE, this.onSkinLoaded, this);
        this.fixCustomViews();
    }


    //下面的代码必须在已经加入场景后才能运行，否则会报错
    protected onAddedToStage() {
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
        this.loadResource().then(() => {
            this.skinName = "resource/skins/scene/MainScene.exml";
        });
    }

    private async loadResource() {
        const loadingView = new LoadingUI();
        this.stage.addChild(loadingView);
        await RES.loadConfig("resource/default.res.json", "resource/");
        await this.loadTheme();
        await RES.loadGroup("preload", 0, loadingView);
        this.stage.removeChild(loadingView);
    }

    private async loadTheme() {
        return new Promise((resolve, reject) => {
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);

        });
    }

    private cet4Image: eui.Image;
    private smallPersonImage: eui.Image;
    private bigPersonImage: eui.Image;
    private surpriseImage: eui.Image;



    public async onSkinLoaded() {
        this.cet4Image.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            SceneManager.instance.addScene(new CET4Scene());
        }, this);

        this.surpriseImage.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
            SceneManager.instance.addScene(new SurpriseScene());
        }, this);

        var tw1 = egret.Tween.get(this.smallPersonImage);
        tw1.to({ x: 300 }, 30000).to({ x: 389 }, 30000).to({ x: 300 }, 30000).to({ x: 389 }, 30000);
        var tw2 = egret.Tween.get(this.bigPersonImage);
        tw2.to({ x: 370 }, 30000).to({ x: 278 }, 30000).to({ x: 370 }, 30000).to({ x: 278 }, 30000);
        if (egret.Capabilities.runtimeType == egret.RuntimeType.WXGAME) {
            let userHelper = UserHelper.instance;
            if (!userHelper.hasLogin()) {
                let firstLogin = await userHelper.login();
                if (firstLogin) {
                    wx.showModal({
                        content: "由于是首次登陆，请点击红色按钮并授权以初始化用户信息"
                    });
                    let button = wx.createUserInfoButton({
                        type: 'text',
                        text: '获取用户信息',
                        style: {
                            left: 10,
                            top: 76,
                            width: 200,
                            height: 40,
                            lineHeight: 40,
                            backgroundColor: '#ff0000',
                            textAlign: 'center',
                            fontSize: 16,
                            borderRadius: 4
                        }
                    })
                    button.onTap((res) => {
                        if (res.userInfo) {
                            userHelper.initUserInfo(res.userInfo);
                            button.hide();
                            wx.showToast({
                                title: "初始化完成"
                            })
                        } else {
                            wx.showModal({
                                content: "该授权只会获取用户名、性别、头像等非敏感信息，请尽量同意授权请求～"
                            })
                        }
                    })
                } else {
                    wx.showToast({
                        title: "登录成功！"
                    });
                }
            }
            if (DEBUG) console.log(await userHelper.getUserInfo());
        }
    }

    /**
     * 所有在EXML中使用的自定义组件均需要这样声明一次，否则在微信小游戏中会找不到类定义
     */
    public fixCustomViews() {
        window["FlowLayout"] = FlowLayout;
        window["BlankView"] = BlankView;
    }
}